<?php

/**
 * @author Lam Vu Nhat
 * @copyright 2011
 */
class Muare_Storage_Curl
{
	/**
	 * Current cURL resource
	 *
	 * @var resource
	 */
	protected $_ch = false;

	/**
	 * Last received HTTP code
	 *
	 * @var integer
	 */
	protected $_code = 0;

	/**
	 * Config
	 *
	 * @var array
	 */
	protected $_config = array(
		'server' => 'http://127.0.0.1',
        'secretKey' => 'd1b6ab808d6f64268ebdc25777f7bcd8',
        'preFix' => 'muare'
	);

	/**
	 * Constructor
	 */
	public function __construct()
	{
		if (XenForo_Application::get('options')->MRStorageServerIP)
		{
			$this->_config['server'] = 'http://' . XenForo_Application::get('options')->MRStorageServerIP;
		}
		if (XenForo_Application::get('options')->MRStorageSecretKey)
		{
			$this->_config['secretKey'] = XenForo_Application::get('options')->MRStorageSecretKey;
		}
		if (XenForo_Application::get('options')->MRStoragePrefix)
		{
			$this->_config['preFix'] = XenForo_Application::get('options')->MRStoragePrefix;
		}

		// Initialize a cURL session
		$this->_ch = curl_init();
	}

	/**
	 * Upload file to server
	 *
	 * @param string $filePath
	 * @param string $filedata
	 */
	public function upload($filePath, $fileData)
	{
		return $this->_doRequest($this->_config['server'] . '/upload',
			array(
				'filename' => $this->_config['preFix'] . '/' . $filePath,
				'filedata' => '@' . $fileData,
				'secret_key' => $this->_config['secretKey']
			), true);
	}

	/**
	 * Delete file from server
	 *
	 * @param string $filePath
	 */
	public function delete($filePath)
	{
		return $this->_doRequest(
			$this->_config['server'] . "/delete?filename=" . $this->_config['preFix'] . '/' . $filePath
                . "&secret_key=" . $this->_config['secretKey']);
	}

	/**
	 * Perform a cURL
	 *
	 * @param str $url
	 * @param mixed $vars
	 * @param booling $methodPost
	 */
	protected function _doRequest($url, $vars = null, $methodPost = false)
	{
		curl_setopt($this->_ch, CURLOPT_URL, $url);
		curl_setopt($this->_ch, CURLOPT_HEADER, 0);

		if (isset($_SERVER['HTTP_USER_AGENT']))
		{
			curl_setopt($this->_ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		}

		curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->_ch, CURLOPT_COOKIEJAR, 'cookie.txt');
		curl_setopt($this->_ch, CURLOPT_COOKIEFILE, 'cookie.txt');
		curl_setopt($this->_ch, CURLOPT_HTTPHEADER, array('Expect:'));

		if ($methodPost)
		{
			curl_setopt($this->_ch, CURLOPT_POST, 1);
			curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $vars);
		}

		$result = curl_exec($this->_ch);
		$this->_code = curl_getinfo($this->_ch, CURLINFO_HTTP_CODE);

		if ($result)
		{
			return $result;
		}
		else
		{
			return curl_error($this->_ch);
		}
	}

	/**
	 * @return Last received HTTP code
	 */
	public function getCode()
	{
		return $this->_code;
	}

	/**
	 * Close cURL resource, and free up system resources
	 */
	public function __destruct()
	{
		curl_close($this->_ch);
	}
}
